package org.wendu.wdoa.common;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.TypeMismatchException;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.wendu.wdoa.common.exception.BusinessException;
import org.wendu.wdoa.common.exception.SysException;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
@RestControllerAdvice //该注解说明本类是一个RestController的拦截者，可以对Controller处理方法添加额外逻辑，做增强处理
public class OaControllerAdvice {

    //该注解说明本方法是一个异常处理器，即当被拦截的Controller处理方法发生指定的异常后，即由本方法处理
    @ExceptionHandler(ConstraintViolationException.class)
    public org.wendu.wdoa.common.Result handleConstraintViolationException(ConstraintViolationException e ){
        log.error("参数不匹配！",e);
        Set<ConstraintViolation<?>> set =  e.getConstraintViolations();//获取验证错误集合
        //将验证错误集合中每一个错误的信息取出来，组成一个新的集合，再将新集合中的元素用逗号分隔连接为一个字符串
        String msg = set.stream().map(item->item.getMessage()).collect(Collectors.joining(","));
        msg = msg.substring(0,msg.length()-1);
        return org.wendu.wdoa.common.Result.err(org.wendu.wdoa.common.Result.CODE_ERR_SYS, msg);
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public org.wendu.wdoa.common.Result handleMethodArgumentNotValidException(MethodArgumentNotValidException e){
        log.error("参数不匹配！",e);
        BindingResult bindingResult = e.getBindingResult();//获取参数绑定结果（包括绑定错误信息）
        List<FieldError> fieldErrorList =  bindingResult.getFieldErrors();//获取属错误集合
        String errMsg = fieldErrorList.stream()
                .map(fieldError -> fieldError.getField()+":"+fieldError.getDefaultMessage())
                .collect(Collectors.joining(","));
        return org.wendu.wdoa.common.Result.err(org.wendu.wdoa.common.Result.CODE_ERR_SYS, errMsg);
    }

    @ExceptionHandler(BindException.class)
    public org.wendu.wdoa.common.Result handleBindException(BindException e){
        log.error("参数不匹配！",e);
        BindingResult bindingResult = e.getBindingResult();//获取参数绑定结果（包括绑定错误信息）
        List<FieldError> fieldErrorList =  bindingResult.getFieldErrors();//获取属错误集合
        String errMsg = fieldErrorList.stream()
                .map(fieldError -> fieldError.getField()+":"+fieldError.getDefaultMessage())
                .collect(Collectors.joining(","));
        return org.wendu.wdoa.common.Result.err(org.wendu.wdoa.common.Result.CODE_ERR_SYS, errMsg);
    }

    @ExceptionHandler(SysException.class)//处理自定义系统异常
    public org.wendu.wdoa.common.Result handleSysException(SysException e){
        log.error("系统错误！",e);
        return org.wendu.wdoa.common.Result.err(org.wendu.wdoa.common.Result.CODE_ERR_SYS,"系统升级中......");
    }

    @ExceptionHandler(BusinessException.class)//处理自定义业务异常
    public org.wendu.wdoa.common.Result handleBusinessException(BusinessException e){
        return org.wendu.wdoa.common.Result.err(org.wendu.wdoa.common.Result.CODE_ERR_BUSINESS,e.getMessage());
    }

    @ExceptionHandler(TypeMismatchException.class)//处理参数格式不匹配异常
    public org.wendu.wdoa.common.Result handleTypeMismatchException(TypeMismatchException e){
        log.error("系统错误！",e);
        return org.wendu.wdoa.common.Result.err(org.wendu.wdoa.common.Result.CODE_ERR_SYS,"系统升级中......");
    }

    @ExceptionHandler(Exception.class)//处理遗漏的异常
    public org.wendu.wdoa.common.Result handleException(Exception e){
        log.error("系统错误！",e);
        return org.wendu.wdoa.common.Result.err(org.wendu.wdoa.common.Result.CODE_ERR_SYS,"系统升级中......");
    }


}
